<?php



namespace app\api\controller\pifa;





use app\common\controller\Api;

use app\api\model\Cart as CartModel;

use think\Db;   

class Cart extends Api

{



    protected $noNeedLogin = [];

    protected $noNeedRight = ['*'];



     public function index()

    {



        $post = $this->request->post();







        $data = CartModel::info($post);

        $user_vip = [];

        if($this->auth->id){

              $map2['user_id'] = $this->auth->id;
            $map2['status'] = '1';
            $user_vip = Db::table('fa_pifa_md')->where($map2)->find();

        }

        $list = [];

        $i = 0;
        $ids = [];
        foreach ($data as $key => $value) {

            if (isset($value['goodsitem']) && isset($value['goodsitem']['goods_price']) && isset($value['goods']) && isset($value['goods']['goods_id']) && $value['goods']['goods_status'] == '10') {

                $list[$i] = $value;

                $i++;

            }else{
                array_push($ids,$value['id']);
            }

            

        }


        if (!empty($ids)) {
            
            $map_del['id'] = ['in',$ids];
            $map_del['user_id'] = $this->auth->id;

            Db::table('fa_pifa_cart')->where($map_del)->delete();
        }

        if (!empty($user_vip)) {

            foreach ($list as $key => $value) {

                $list[$key]['goodsitem']['goods_price'] = $value['goodsitem']['goods_price2'];

            }

        }







        foreach ($list as $key => $value) {

            if ($value['goods']['is_zhekou'] == 1) {

               $list[$key]['goodsitem']['goods_price'] = $value['goods']['zhekou_price'];

            }

            

        }

        if ($this->auth->id) {
           $user = Db::table('fa_user')->find($this->auth->id);
           $baojia_id = 0;
           if (!empty($user['baojia_id'])) {
               $baojia_id = $user['baojia_id'];
           }

            foreach ($list as $key => $value) {
                if (!empty($baojia_id) && !empty($value['goodsitem']['baojia'])) {
                    $baojia = json_decode($value['goodsitem']['baojia'],true);
                    if (isset($baojia[$baojia_id])) {
                        $price = bcadd($value['goodsitem']['goods_price'], $baojia[$baojia_id],2);

                        if ($price <= 0) {
                            $price = 0.01;
                        }
                        $list[$key]['goodsitem']['goods_price'] = $price;
                    }
                }


                if (!empty($post['shangjia_id']) && !empty($value['goodsitem']['baojia_quyu'])) {
                    $baojia_quyu = json_decode($value['goodsitem']['baojia_quyu'],true);

                    $price2 =  $list[$key]['goodsitem']['goods_price'];
                    if (isset($baojia_quyu[$post['shangjia_id']])) {
                        $price2 = bcadd($price2, $baojia_quyu[$post['shangjia_id']],2);

                        if ($price2 <= 0) {
                            $price2 = 0.01;
                        }
                        $list[$key]['goodsitem']['goods_price'] = $price2;  
                    }
                }
              

            }



        }



        $this->success('我的购物车', $list);

    }



    public function add()

    {

        $params = $this->request->post();

        

        // 表单验证

      

        $goodsList = $params['goods_list'];

        $this->success('已添加', CartModel::add($goodsList,$params['mendian_id']));

    }



    public function edit()

    {

        $params = $this->request->post();



     

      

        $this->success('', CartModel::edit($params));

    }



}